package com.modern.js.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.modern.js.entity.Employee;
import com.modern.js.pojo.AssistPojo;
import com.modern.js.service.IEmployeeService;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-03-23
 */
@RequestMapping("/emp")
@RestController
public class EmployeeController {
    @Autowired
    private IEmployeeService iEmployeeService;

//    @GetMapping("/list")
//    public Map getList(){
//        Map<String,Object> map = new HashMap<String,Object> ();
////        Page<Employee> page = new Page<Employee>();
////        page.setSize(pageSize);
////        page.setCurrent(current);
////        jsonObject.put("count", count);
////        jsonObject.put("data", list);
////        jsonObject.put("code", 0);
//
//        map.put("data",iEmployeeService.list());
//        map.put("code",0);
//        return map;
//    }

    /**
     *
     * @param page 当前页码
     * @param limit 每页的记录数
     * @return
     */
    @GetMapping("/list")
    public Map getList(Integer page,Integer limit,String empName,String createTime){
        Map<String,Object> map = new HashMap<String,Object> ();
        Page<Employee> employeePage = new Page<Employee>();
        employeePage.setSize(limit);//设置每页记录数
        employeePage.setCurrent(page);//设置当前页码
        IPage<Employee> iPage = iEmployeeService.selectList(employeePage,empName,createTime);
        if (iPage.getTotal()!=0){
            map.put("msg","查询成功！");
        }else {
            map.put("msg","查询失败！");
        }
        map.put("data",iPage.getRecords());
        map.put("count",iPage.getTotal());
        map.put("code",0);
        return map;
    }

    @DeleteMapping("/del/{empId}")
    public Map delEmployee(@PathVariable Integer empId){
        Employee employee = new Employee();
        employee.setEmpId(empId);
        employee.setIsDel(0);
//        boolean result = iEmployeeService.updateById(employee);
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",iEmployeeService.updateById(employee));
        return result;
    }

    @PostMapping("/add")
    public int register(Employee employee,Integer roleId){
        return iEmployeeService.register(employee, roleId);
    }

    @PutMapping("/update")
    public int employeeUpdate(Employee employee,Integer roleId,Integer deptId){
        int result = iEmployeeService.employeeUpdate(employee, roleId,deptId);
        System.out.println("值："+employee);
        if (result==3){
            System.out.println("修改成功！");
            return result;
        }else {
            System.out.println("修改失败！");
        }
        return result;
    }

    @GetMapping("/byName")
    public String selectByName(String empName,Model model){
        Employee employee = iEmployeeService.selectByName(empName);
        model.addAttribute("employee",employee);
        System.out.println("值的查询："+empName);
        return "/personage";
    }

    @GetMapping("/selectConut")
    public int selectConut(){
        System.out.println("你好员工！");
        Integer num = iEmployeeService.count();
        System.out.println("员工总人数："+num);
        return num;
    }

    @PutMapping("/updatePerson")
    public int updatePerson(Employee employee){
        System.out.println("前台的值："+employee);
        int result = iEmployeeService.updatePerson(employee);
        return result;
    }

}
